import { EVENT_NAME_ENUM } from "../base/EnumIndex";
import { EventManager } from "../manager/EventManager";

const { ccclass, property } = cc._decorator;


@ccclass
export class ProgressItem extends cc.Component {
  bar: cc.ProgressBar = null;
  /** 进度总数 */
  total: number = 10;
  titleLbl: cc.Label = null;

  protected onLoad(): void {
    EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this)
    this.bar = this.node.getChildByName("progressBar").getComponent(cc.ProgressBar);
    this.titleLbl = this.node.getChildByName("title").getComponent(cc.Label);
  }
  protected onDestroy(): void {
    EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this)
  }
  /** 进度总数，每次＋1 */
  setTotal(total: number) {
    this.total = total
  }
  /** 标题字 */
  setTitle(title: string) {
    this.titleLbl.string = title + "：";
  }

  finish(finishStr: string) {
    if (typeof finishStr !== "string") { return }
    if (finishStr.includes("addState_")) {
      this.bar.progress += 1 / this.total;
    } else if (finishStr.includes("removeState_")) {
      this.bar.progress -= 1 / this.total;
    } else if (finishStr.includes("clear")) {
      this.clear();
    }
  }

  /** 进度清0 */
  clear() {
    this.bar.progress = 0;
  }
}